package com.rmfyzxfw.caseprocess.boot.threadpool;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Slf4j
@Configuration
public class AsyncConfig {

    private final int coreSize = 100;

    private final int maxSize = 100;

    private final int queueSize = 1000000;

    @Bean("task")
    public AsyncTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(coreSize);
        taskExecutor.setCorePoolSize(maxSize);
        taskExecutor.setQueueCapacity(queueSize);
        taskExecutor.setRejectedExecutionHandler(((r, e) -> {
            String s = "Task " + r.toString() +
                    " rejected from  " +
                    e.toString();
            log.error(s);
        }));
        return taskExecutor;
    }
}
